
require 'Common/define'
require 'Logic/Config'
require 'Common/functions'
require 'Net/itemHandler'

UICharactorPreviewAttribute = {};
HelperFunc.SetUIEnv(UICharactorPreviewAttribute)
local this = UICharactorPreviewAttribute;

this.linkColor = "#"..G.dataTable["t_global"][tostring(185)]["f_string_value"];
this.normalColor ="#FFFFFF";

this.gameObject = nil;
this.transform = nil;
this.LuaBehaviour = nil;

this.hasInit = false;

this.currentHeroData = nil;
this.charactorExpConfig = nil;
this.heroConfig = nil;
this.itemConfig = nil;

this.curCharatorExpConfig = nil;
this.nextCharatorExpConfig = nil;

this.txtLevel = nil;
this.txtAtk = nil;
this.txtDef = nil;
this.txtHp = nil;
this.atkTitle = nil
this.hpTitle = nil
this.defTitle = nil
--this.btnDetails = nil;

this.objLevelGroup = nil;
this.txtPower = nil

function UICharactorPreviewAttribute.Init(heroData)
    local name,package = getUIName(PanelResNames.UICharactorPreviewAttribute);
    currentHeroData = heroData
    if charactorExpConfig ==nil then
        charactorExpConfig = G.dataTable["t_characterexp"];
    end
    if itemConfig == nil then
        itemConfig =  G.dataTable["t_item"]
    end
    if heroConfig == nil then
        heroConfig = G.dataTable["t_hero"]
    end
    panelMgr:ShowUI(name,package, this.onCreate,Config.useLocalUI);
end
---@param obj FairyGUI.GComponent
function UICharactorPreviewAttribute.onCreate(obj)
    --gameObject = obj;
    --transform = obj.transform;
    --LuaBehaviour = transform:GetComponent('LuaBehaviour');

    if not hasInit then
        hasInit = true;
        txtLevel = obj:GetChild("number")
        txtAtk = obj:GetChild("fight"):GetChild("number")
        txtDef = obj:GetChild("fy"):GetChild("number")
        txtHp = obj:GetChild("life"):GetChild("number")
        --btnDetails = obj:GetChild("informationButton")
        txtPower = obj:GetChild("power_number")
        atkTitle = obj:GetChild("fight"):GetChild("name")
        hpTitle = obj:GetChild("life"):GetChild("name")
        defTitle = obj:GetChild("fy"):GetChild("name")
        atkTitle.text = "攻击"
        hpTitle.text = "生命"
        defTitle.text = "防御"
        this.addEvent();
    end

    this.refresh();
end

function UICharactorPreviewAttribute.addEvent()
    --btnDetails.onClick:Add(this.onClickDetails)
    --LuaBehaviour:AddClick(btnDetails.gameObject,this.onClickDetails);
end

function UICharactorPreviewAttribute.removeEvent()
    --btnDetails.onClick:Clear()
    --LuaBehaviour:RemoveClick(btnDetails.gameObject);
end

function UICharactorPreviewAttribute.LocalListenEvent()
    ListenEvent(Config.EventType.Fresh_Currency ,this.reGetData);
    ListenEvent(Config.EventType.Fresh_Level,this.reGetData);
    ListenEvent(Config.EventType.Fresh_Star,this.reGetData);
    ListenEvent(Config.EventType.Fresh_Skill,this.reGetData);
    ListenEvent(Config.EventType.Fresh_Mercenary,this.reGetData);
    ListenEvent(Config.EventType.Fresh_HeroLink,this.reGetData);

end

function UICharactorPreviewAttribute.LocalCloseEvent()
    CloseEvent(Config.EventType.Fresh_Currency,this.reGetData);
    CloseEvent(Config.EventType.Fresh_Level,this.reGetData);
    CloseEvent(Config.EventType.Fresh_Star,this.reGetData);
    CloseEvent(Config.EventType.Fresh_Skill,this.reGetData);
    CloseEvent(Config.EventType.Fresh_Mercenary,this.reGetData);
    CloseEvent(Config.EventType.Fresh_HeroLink,this.reGetData);
end

function UICharactorPreviewAttribute.OnDestroy()
    this.removeEvent();
end

function UICharactorPreviewAttribute.OnEnable()
    this.LocalListenEvent();
end

function UICharactorPreviewAttribute.OnDisable()
    this.LocalCloseEvent();
end

function UICharactorPreviewAttribute.reGetData()
    if currentHeroData~=nil then
        currentHeroData = logicMgr.HeroManager.getHeroDatabyPartnerId(tostring(currentHeroData["partnerId"]),true);
        this.refresh();
    end
end

function UICharactorPreviewAttribute.refresh()
    if currentHeroData==nil or charactorExpConfig==nil then
        logError("配置为空，或英雄信息为空");
        return;
    end

    nextCharatorExpConfig = charactorExpConfig[MakeTopLevelId(currentHeroData["level"]+1)];

    print(currentHeroData["level"], currentHeroData["star"])
    local curSerchID = currentHeroData["partnerModelId"]*1000+currentHeroData.star
    local config = G.dataTable["t_herorank"][tostring(curSerchID)]
    while config.f_HeroStarNext~=0 do
        config = G.dataTable["t_herorank"][tostring(config.f_HeroStarNext)]
    end
   -- logicMgr.HeroManager.processHeroBaseAttr(currentHeroData, currentHeroData["level"], currentHeroData["star"])
    local maxlevel = dataTable.getGlobalInt(276)
    txtLevel.text = string.TextColor(maxlevel,normalColor);

    local attrData = currentHeroData;
    logicMgr.HeroManager.processHeroBaseAttr(attrData,maxlevel,config.f_HeroStar)
    local num = logicMgr.SkillManager.SkillAddPower(currentHeroData.partnerModelId,300, 150)
    txtPower.text = getNumString((1+num)*logicMgr.HeroManager.processPower(attrData.allAttribute[EnumConst.AttributeTypeEnum.BASE],currentHeroData.type));
    txtAtk.text = attrData.allAttribute[EnumConst.AttributeTypeEnum.BASE][EnumConst.FighterAttributeEnum.ATK];
    txtDef.text = attrData.allAttribute[EnumConst.AttributeTypeEnum.BASE][EnumConst.FighterAttributeEnum.DEF];
    txtHp.text = attrData.allAttribute[EnumConst.AttributeTypeEnum.BASE][EnumConst.FighterAttributeEnum.HP];
end


function UICharactorPreviewAttribute.onClickDetails()
    showUI(PanelNames.UICharactorAttributeDetails,currentHeroData,true);
end

return UICharactorPreviewAttribute;
